﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using HA.Data;

using System.Windows.Controls;

namespace HA.ModFlow.Application
{
    public class Scatter2dCollectionViewModel : MDTVIViewModel
    {

        public Scatter2dCollectionViewModel(ProjectDataViewModel parent, Scatter2DCollection collection)
            : base(parent, false)
        {



            this._collection = collection;


            this._contextMenu = new System.Windows.Controls.ContextMenu();

            //MenuItem mi = new MenuItem();
            //mi.Header = "测试";

            //this._contextMenu.Items.Add(mi);



            this._imageSource = VMImageSource.Scatter2dCollection;


            this.LoadChildren();



 
        }



        Scatter2DCollection _collection;

        public Scatter2DCollection Collection
        {
            get { return _collection; }
            set { _collection = value; }
        }



        public override string Desc
        {
            get
            {
                return "二维离散点";
            }
        }

        protected override void LoadChildren()
        {
            
            foreach (Scatter2D item in this._collection.Datas)
            {
                Scatter2dViewModel svm = new Scatter2dViewModel(this,item);

                this.Children.Add(svm);
                
            }

            _collection.CollectionChanged += new EventHandler<CollecionChangedEventArgs>(collection_CollectionChanged);
           
           
        }

        void collection_CollectionChanged(object sender, CollecionChangedEventArgs e)
        {
            switch (e.Type)
            {
                case CollectionChangedType.ADD:
                     Scatter2dViewModel svm = new Scatter2dViewModel(this,(Scatter2D)e.Child);

                   this.Children.Add(svm);

                   svm.IsSelected = true;

                    break;
                case CollectionChangedType.REMOVE:

                    foreach (Scatter2dViewModel item in this.Children)
                    {
                        if (item.ScatterData == (Scatter2D)e.Child)
                        {
                            int index = this.Children.IndexOf(item);

                            this.Children.Remove(item);

                            if (index > 0)
                                this.Children[index - 1].IsSelected = true;
                            else if (this.Children.Count > 0)
                                this.Children[0].IsSelected = true;

                            return;
                            
                        }
                        
                    }

                    break;
                case CollectionChangedType.MOVE:


                    break;
                case CollectionChangedType.REPLACE:
                    break;
                case CollectionChangedType.RESET:
                    break;
                default:
                    break;
            }


            ProjectDataViewModel parent = (ProjectDataViewModel)this.Parent;

            parent.CollectionView.Refresh();

        }


        protected override void OnSelectedChanged()
        {
            base.OnSelectedChanged();

            if (this.IsSelected)
            {
                UIModule.DefaultModule.RibbonV2ViewModel.ViewOpViewModel.ToolVM.CurrentTool = APPTOOLMODE.SCATTER2D;

                //UIModule.DefaultModule.RibbonV2ViewModel.ViewOpViewModel.ToolVM.IsScatterMode = true;
                
            }
        }



    }
}
